import os
from PyPDF2 import PdfMerger

# target_path = r'C:\Users\Administrator\Desktop\合并pdf文件'  # pdf目录文件

path = "D:\pdf"
# target_path = os.path.dirname(path)
pdf_lst = [f for f in os.listdir(path) if f.endswith('.pdf')]
pdf_lst = [os.path.join(path, filename) for filename in pdf_lst]

file_merger = PdfMerger()
for pdf in pdf_lst:
    file_merger.append(pdf, import_outline=False)  # 合并pdf文件

file_merger.write(r"D:\pdf\合并文件.pdf")